import Vue from 'vue'
import App from './App'
import common from './static/js/common.js'
import sqlitedb from "@/static/js/sqlitedb.js";//数据库操作类

import MescrollBody from "@/components/mescroll-uni/mescroll-body.vue"
import MescrollUni from "@/components/mescroll-uni/mescroll-uni.vue"
import AD from '@/js_sdk/ad/ad.js';

//全局注册
Vue.component('mescroll-body', MescrollBody)
Vue.component('mescroll-uni', MescrollUni)


const url = 'http://test.recharge.607068.com'  
const systeminfo = uni.getSystemInfoSync(); //获取设备信息
const _that = Vue.prototype;
//是否为测试版APP
Vue.prototype.$AppisTest = true;
//全局计算属性
Vue.prototype.$common = common;
//全局引入广告插件
Vue.prototype.$ttylAd = AD;
//全局数据库操作类
Vue.prototype.$sqliteDb = sqlitedb

//全局requst请求数据
Vue.prototype.$HttRequest = function(data, methods = "GET", isload = false) {
	if (isload) {
		uni.showLoading({
			title: '加载中...',
			mask: true
		});
	}
	return new Promise((resolve, reject) => {
		//console.log( 'start',data.url,Date.parse(new Date()) / 1000)
		uni.request({
			url: `${url}` + data.url,
			data: data.param,
			method: methods,
			header: {
				"device": systeminfo.platform, //设备名称(Android,iOS)
				"device-type": systeminfo.model, //设备型号(iphone 9)
				"device-width": systeminfo.windowWidth,
				"device-height": systeminfo.windowHeight,
				"lang": systeminfo.language,
				"req-time": Date.parse(new Date()) / 1000, //请求时间
				"sys-version": systeminfo.system,
				"content-type": "application/json",
				"access-token": uni.getStorageSync('dxzk_access_token'),
				"X-Requested-With": "XMLHttpRequest",
				"app-version":(plus.runtime.version).split('.').join('')
			},
			success(ret) {
				//console.log( 'end',data.url,Date.parse(new Date()) / 1000)
				uni.hideLoading();
				//用户accss_token过期直接退出登陆
				if (ret.data.status === 500000) {
					_that.$Expire('请先登陆');
					return;
				}
				resolve(ret.data);
			},
			fail(error) {
				uni.hideLoading();
				reject(error);
			}
		})
	})
}

//登陆成功存储用户
Vue.prototype.$Saveinfo = function(dxzk_access_token, user_info, user_phone) {
	uni.setStorageSync('dxzk_access_token', dxzk_access_token);
	uni.setStorageSync('user_info', user_info);
	//uni.setStorageSync('user_phone', user_phone);
	_that.$ShowMsg('欢迎使用', '/static/image/modelicon.png')
	setTimeout(() => {
		//获取设备cid用于推送
		_that.$getcid();
		uni.switchTab({
			url: '/pages/index/index'
		})
	}, 3000);
}

//自动退出
Vue.prototype.$Expire = function(msgs) {	
	//通知后台清楚当前账号下设备的cid
	_that.$getcid('false'); 
	//回复默认用户信息
	uni.setStorageSync('user_info', _that.$Initdata);
	//提示
	_that.$ShowMsg(msgs, '/static/image/modelicon.png'); 
	//清空内存后跳转首页
	setTimeout(() => {
		uni.clearStorageSync();
		uni.reLaunch({
			url: '/pages/index/index',
			animationType: "slide-in-bottom" 
		})
	}, 3000);
}

//全局图片上传
Vue.prototype.$Uploadimg = function(tempFilePaths) {
	uni.showLoading({
		title: '图片上传中...',
		mask: true
	})
	console.log(tempFilePaths)
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: `${url}/setting/uploadImage`,
			name: 'files',
			filePath: tempFilePaths,
			header: {
				"device": systeminfo.platform, //设备名称(Android,iOS)
				"device-type": systeminfo.model, //设备型号(iphone 9)
				"device-width": systeminfo.windowWidth,
				"device-height": systeminfo.windowHeight,
				"lang": systeminfo.language,
				"req-time": Date.parse(new Date()) / 1000, //请求时间
				"sys-version": systeminfo.system,
				"access-token": uni.getStorageSync('dxzk_access_token'),
				"X-Requested-With": "XMLHttpRequest",
				"app-version":(plus.runtime.version).split('.').join('')
			},
			formData: {},
			success(ret) {
				uni.hideLoading();
				console.log(ret.data)
				resolve(ret.data);
			},
			fail(error) {
				uni.hideLoading();
				reject(error);
			}
		})
	})
}

//全局提示数据
Vue.prototype.$ShowMsg = function(msg, icon = null,mask=true) {

	if (icon == null) {
		uni.showToast({
			title: msg,
			duration: 2500,
			icon: "none",
			mask: mask
		})
	} else {
		if(icon=='success'){
			icon = '/static/image/success.png'
		}else if(icon=='error'){
			icon = '/static/image/error.png'
		}
		uni.showToast({
			title: msg,
			duration: 2500,
			// icon: 'success',
			image: icon,
			mask: mask
		})
	}

}

//获取当前时间戳
Vue.prototype.$timer = function() {
	return Date.parse(new Date()) / 1000;
}

//用户如果登陆直接进入首页
Vue.prototype.$isLogin = function() {

	//查询请求token是否存在
	let access_token_value = uni.getStorageSync('dxzk_access_token');
	if (access_token_value) {
		console.log('自动登陆')
		uni.switchTab({
			url: '/pages/index/index'
		})
	}
}


//修改内存中字段的值
Vue.prototype.$changefield = function(fields, values) {
	return new Promise((resolve, reject) => {
		let userinfos = uni.getStorageSync('user_info');
		userinfos[fields] = values;
		try {
			uni.setStorageSync('user_info', userinfos);
			resolve(true);
		} catch (e) {
			reject(e);
		}
	})
}


//创建内存表
Vue.prototype.$storetable = function(tables, fields, values) {
	return new Promise((resolve, reject) => {
		let tableinfo = uni.getStorageSync(tables);
		if (!tableinfo) {
			//内存不存在数据表直接创建
			uni.setStorageSync(tables);
		}
		tableinfo[fields] = values;
		try {
			uni.setStorageSync(tables, tableinfo);
			resolve(true);
		} catch (e) {
			reject(e);
		}
	})
}

//初始化抽奖内存数据

Vue.prototype.$luckinit = {
	type: 'goods', //goods','red_envelope' '实物/红包',
	pattern: 1, //1预览模式 2已发布模式
	paytotal: 0, //计算总价格
	//发布者的信息
	sponsor: uni.getStorageSync('user_info'),
	prizeimgarr: [{
		red_envelope_type: 'count', //'红包类型:count:总金额,sign单个红包
		red_envelope_money: '', //红包金额
		grant: 'address', //'address','contact_me 发放方式'
		title: '',
		count: 1, //数量
		thumb: '../../static/image/topdefault.png',
		//发起人信息
		contactinfo: {
			id: '',
			attention_text: '', //需要复制的内容
		},
		goods_id: '', //商城商品id,
		goods_price: 0, //金额
		coupon_id: '', //优惠券ID
	}],
	//抽奖用户配置参数
	luckconfig: {
		//抽奖说明
		explaininfo: {
			explanation: '', //说明详情
			//发起人信息
			contactinfo: {
				id: '',
				attention_text: '', //需要复制的内容
			}

		},
		//视频源
		videourl:'',
		//真实视频源地址
		rel_videourl:'',
		//开奖方式
		drawopen: {
			draw_type: "timing", //'timing','manchu','manual' 开奖方式
			start_time: "", //开奖时间 
			total_people: 0, //参与人数
		},
		//是否开启高级选择
		advanced: false,
		//高级选项
		senior: {
			//图文
			details: {
				detailtext: '',
				detailimag: [],
				detailsinfo: '', //需要渲染的富文本数据
			},
			share: true, //是否分享1,允许,0不允许',
			boost: false, //允许分享的情况,是否允许好友助力:1,允许,0不允许
			//好友助力配置
			boostconfig: {
				boost_multiple: 1, //助力倍数
				boost_password: '', //助力口令
				help_red_envelope: false, //是否开启助力红包
				//助力红包配置
				help_red_envelope_config: {
					help_red_type: 'ordinary', //助力红包类型:ordinary普通红包,sign单个红包'
					help_red_amount: 0, //助力红包红包金额
					help_red_count: 0, //助力红包-个数
					help_red_condition: 0, //助力红包 最少拉多少人
				},
			},
			//评论内容
			comment: {
				comment: false, //是否开启评论:0关闭,1开启
				comment_audit: false, //开启评论审核:0关闭,1开启
				commentlayer: false, //显示开启评论遮罩
			},
			//短视频粉丝参与设置
			shortvideo_cond:false,
			//公众号粉丝参与设置
			joinfans: false,
			//参与条件设置
			joincondition: false,
			joinconfig: {
				limited_sex: 0, //限定性别:0不限制,1.男,2女
				limited_password: '', //抽奖口令
				limited_real_name: 0, //实名抽奖,0不需要实名, 1需要实名
				wx_mp_name: '', //设置公众号名称
				wx_mp_key: '', //公众号回复口令
				wx_mp_id: '', //一键授权公众号ID
				short_video_platform:0, //1抖音 // 2 快手
				short_video_userid:'',
				short_video_account:'',
				short_video_id:'',
			}

		}
	}
}

//更新首页APP图标红点
Vue.prototype.$redpoint = function(param) {
	let apptouchday = uni.getStorageSync('apptouchday');
	if (apptouchday) {
		apptouchday[param.index] = true;
		uni.setStorageSync('apptouchday', apptouchday);
	}
}

//获取设备的cid用于推送消息
Vue.prototype.$getcid = function(type = 'true') {
	let clienttimer = setInterval(() => {
		let clientinfo = plus.push.getClientInfo()
		if(type=='false') clientinfo.clientid='ttyl'
		if (clientinfo.clientid) {
			clearInterval(clienttimer)
			let cidpostdata = {
				url: '/setting/cid',
				param: {
					cid: clientinfo.clientid,
					type: type
				}
			}
			//请求后台完成
			_that.$HttRequest(cidpostdata, 'POST').then((res) => {
				if (res.status === 10000) {
					console.log(cidpostdata) 
				} else {
					_that.$ShowMsg(res.message)
				}
			}).catch((error) => {
				//异常错误
				_that.$ShowMsg('statusCode：' + error.errMsg);
			})
		}
	}, 500);
}

//观看广告获取奖励
Vue.prototype.$rewardAd = function(type=null) {
	return new Promise((resolve, reject) => {
		_that.$HttRequest({
			url: '/task/videoAd',
			param: {type:type}
		}).then((res) => {
			if (res.status === 10000) {
				//成功返回
			} else {
				_that.$ShowMsg(res.message)
			}
			resolve(res)
		}).catch((error) => {
			//异常错误
			_that.$ShowMsg('statusCode：' + error.errMsg);
			reject(error)
		})
	})
	
}
//用户默认信息
Vue.prototype.$Initdata = {
	level: 0,
	nickname: "天天有礼用户",
	phone: "",
	avatar: "/static/image/davatar.png", 
	money: 0,
	integral: 0,
	lottery_app:true,//引导
	recommend_id:0,
}

//判断是否需要弹出登陆提示
Vue.prototype.$Logintip = function(){
	if(uni.getStorageSync('dxzk_access_token')){
		return false
	}else{
		return true
	}
}

//用户登陆提示
 Vue.prototype.$Userlogin =function(){
	uni.showModal({ //提醒用户更新
		title: "登陆提示",
		content:'请登陆后继续操作' ,
		showCancel: true,
		confirmText:'立即登陆',
		success: (ret) => {
			console.log(ret)
			//用户点击确认后触发 
			if (ret.confirm) {
				uni.reLaunch({
					url:'/pages/login/login',
				})
			}
		}
	})
}
//图片预览
Vue.prototype.$Preimage = function(imgarray,index=0){
	//将字符串处理成数组
	if (typeof imgarray == 'string') {
		let img = []
		img.push(imgarray)
		imgarray = img
	}
	uni.showLoading({
		title: '图片载入中...',
		mask: true
	})
	uni.previewImage({
		indicator: "default",
		current: index, //为当前显示图片的链接/索引值
		urls: imgarray,
		success(res) {
			uni.hideLoading();
			console.log(res);
		},
		fail(error) {
			uni.hideLoading();
			console.log(error);
		}
	})
}

 Vue.prototype.$Userlogin =function(){
	uni.showModal({ //提醒用户更新
		title: "登陆提示",
		content:'请登陆后继续操作' ,
		showCancel: true,
		confirmText:'立即登陆',
		success: (ret) => {
			console.log(ret)
			//用户点击确认后触发 
			if (ret.confirm) {
				uni.reLaunch({
					url:'/pages/login/login',
				})
			}
		}
	})
}

Vue.config.productionTip = false

App.mpType = 'app'

const app = new Vue({
	...App
})
app.$mount()
